package com.example.fengyungo.service;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.GetObjectRequest;
import com.alibaba.sdk.android.oss.model.GetObjectResult;
import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.OssConstant;
import com.example.fengyungo.domain.Driver;
import com.example.fengyungo.utils.BitmapUtils;
import com.example.fengyungo.utils.DriverUtils;
import com.example.fengyungo.utils.OssUtils;
import com.example.fengyungo.utils.SharedPreferencesUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;


public class CommonIntentService extends IntentService {

    private static final String TAG = "CommonIntentService";

    public CommonIntentService() {
        super("CommonIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d(TAG, "onHandleIntent: ");
        Driver driver = SharedPreferencesUtils.getDriverInfoInService(this);
        String avatarURL = driver.getAvatar();
        Bitmap avatar = OssUtils.getBitmapFromOss(this,avatarURL);
        Log.d(TAG, "onHandleIntent: avatar="+avatar);
        // 将图片存入SharedPreferences
        BitmapUtils.saveBitmapToSharedPreferences(avatar, this, DriverConstant.AVATAR);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}